package com.example.demo.practice.enterprise;

import android.content.Intent;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.innerclass.XFormatter;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ScoringHistoryShow extends IBaseActivity {

    private LineChart mChart;
    private Spinner year;
    private Spinner month;
    private Spinner week;

    private static String YEARSCORE="年度评分";
    private static String QUARTERSCORE="季度评分";
    private static String MONTHSCORE="月评分";
    private static String WEEKSCORE="周评分";

    private static final String[] dates = new String[]{"今日", "本周", "本月"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoring_history_show);
        Intent intent = getIntent();
        setTitle(intent.getStringExtra("name"));
        initChart();
        initDropDown();
    }

    private void initChart() {
        mChart = (LineChart) findViewById(R.id.chart1);
        mChart.setNoDataText("无法获取数据");
        LimitLine line=new LimitLine(2.5f,"平均水平");
        line.setLineColor(Color.RED);
        line.setLineWidth(2f);

        mChart.getDescription().setEnabled(true);
        mChart.setTouchEnabled(true);
        mChart.setDragDecelerationFrictionCoef(0.9f);
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);
        mChart.setDrawGridBackground(false);
        mChart.setHighlightPerDragEnabled(true);

        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
        xAxis.setTypeface(Typeface.SERIF);
        xAxis.setTextSize(10f);
        xAxis.setTextColor(Color.BLACK);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(true);
        xAxis.setTextColor(Color.rgb(255, 192, 56));
        xAxis.setGranularity(1f);
        xAxis.setYOffset(-9f);

        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        leftAxis.setTypeface(Typeface.SERIF);
        leftAxis.setTextColor(ColorTemplate.getHoloBlue());
        leftAxis.setDrawGridLines(true);
        leftAxis.setGranularityEnabled(true);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(5f);
        leftAxis.setYOffset(-9f);
        leftAxis.addLimitLine(line);
        leftAxis.setTextColor(Color.rgb(255, 192, 56));

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setEnabled(false);
        setDes(YEARSCORE);
    }

    private void setData(int count, float range) {


        ArrayList<Entry> values = new ArrayList<Entry>();

        // increment by 1 hour
        for (float x = 1; x <= count; x++) {

            float y = getRandom(range, 0);
            values.add(new Entry(x, y)); // add one entry per hour
        }

        LineDataSet set1 = new LineDataSet(values, "本单位");
        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.setColor(ColorTemplate.getHoloBlue());
        set1.setValueTextColor(ColorTemplate.getHoloBlue());
        set1.setLineWidth(2f);
        set1.setDrawCircles(true);
        set1.setDrawValues(true);
        set1.setValueTextColor(Color.BLACK);
        set1.setValueTypeface(Typeface.SERIF);
        set1.setValueTextSize(20f);
        set1.setFillAlpha(65);
        set1.setFillColor(ColorTemplate.getHoloBlue());
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setDrawCircleHole(true);

        // create a data object with the datasets
        LineData data = new LineData(set1);
        data.setValueTextColor(Color.BLACK);
        data.setValueTextSize(9f);
        mChart.setData(data);
    }

    private void setDes(String des){
        Description description=new Description();
        description.setText(des);
        mChart.setDescription(description);
    }

    protected float getRandom(float range, float startsfrom) {
        return (float) (Math.random() * range) + startsfrom;
    }

    private void initDropDown() {
        year = (Spinner) findViewById(R.id.year);
        month = (Spinner) findViewById(R.id.month);
        week = (Spinner) findViewById(R.id.week);
        year.setOnItemSelectedListener(new Listener());
        month.setOnItemSelectedListener(new Listener());
        week.setOnItemSelectedListener(new Listener());
        month.setEnabled(false);
        week.setEnabled(false);

    }


    private class Listener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String yearText = year.getSelectedItem().toString();
            String monthText = month.getSelectedItem().toString();
            String weekText = week.getSelectedItem().toString();
            String[] arr;
            if (yearText.equals("全部")) {
                month.setEnabled(false);
                week.setEnabled(false);
                setDes(YEARSCORE);
                arr = getResources().getStringArray(R.array.year);
            } else if (monthText.equals("全部")) {
                setDes(QUARTERSCORE);
                month.setEnabled(true);
                week.setEnabled(false);
                arr = getResources().getStringArray(R.array.quarter);
            } else if (weekText.equals("全部")) {
                setDes(MONTHSCORE);
                month.setEnabled(true);
                week.setEnabled(true);
                arr = getResources().getStringArray(R.array.month);
            } else {
                setDes(WEEKSCORE);
                month.setEnabled(true);
                week.setEnabled(true);
                arr = getResources().getStringArray(R.array.week);
            }
            XFormatter xForm=new XFormatter(arr);
            mChart.getXAxis().setValueFormatter(xForm);
            setData(arr.length,5);
            mChart.invalidate();
            mChart.animateXY(200,300);

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }




}
